package com.example.city_home_app.Activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Post;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PostSearchActivity extends BaseActivity {
    @BindView(R.id.et_community_search)
    EditText etCommunitySearch;
    @BindView(R.id.ll_community_search)
    LinearLayout llCommunitySearch;
    @BindView(R.id.tv_community_search_cancel)
    TextView tvCommunitySearchCancel;
    @BindView(R.id.rec_communiy_search)
    RecyclerView recCommuniySearch;
    @BindView(R.id.tv_community_search_nothing)
    TextView tvCommunitySearchNothing;

    private SwipeRefreshLayout refreshLayout;
    private ApiModel model = new ApiModel();
    private Adapter_Post adapter_post;
    private String search = "";

    private RetrofitListener<List<Down_Post>> searchListener = new RetrofitListener<List<Down_Post>>() {
        @Override
        public void onResponse(List<Down_Post> listener) {
            if (listener.size() != 0) {
                tvCommunitySearchNothing.setVisibility(View.GONE);
                adapter_post.setKey(search);
                adapter_post.setList(listener);
                adapter_post.notifyDataSetChanged();
                refreshLayout.setRefreshing(false);
            } else {
                refreshLayout.setVisibility(View.GONE);
                tvCommunitySearchNothing.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_community_search;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);

        Window window = getWindow();
        //如果系统5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0xffEBEBEB);
        }

        recCommuniySearch = (RecyclerView) findViewById(R.id.rec_communiy_search);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recCommuniySearch.setLayoutManager(layoutManager);
        adapter_post = new Adapter_Post(this);
        recCommuniySearch.setAdapter(adapter_post);
        recCommuniySearch.setItemAnimator(new DefaultItemAnimator());

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_community_search);
        refreshLayout.setColorSchemeColors(0xff49afcd);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        etCommunitySearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    search = etCommunitySearch.getText().toString();
                    if (!search.equals("")) {
                        refreshLayout.setRefreshing(true);
                        refreshLayout.setVisibility(View.VISIBLE);
                        refresh();
                    } else {
                        UIUtils.showToast(PostSearchActivity.this, "搜索内容不能为空", UIUtils.LENGTH_SHORT);
                    }
                    View view = getWindow().peekDecorView();
                    //关闭软键盘
                    if (view != null) {
                        InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }


    @OnClick(R.id.tv_community_search_cancel)
    public void onViewClicked() {
        finish();
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        model.searchpost(search, BaseActivity.getUserinfo().getCityid(), searchListener);
                    }
                });
            }
        }).start();
    }

}
